<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>python基本语法 | heit0ng</title><meta name="author" content="heit0ng"><meta name="copyright" content="heit0ng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="看了下课表这学期要学python了，我就把之前学的时候做的笔记传上来吧 比C语言之类确实简单不少，所以笔记做得很简陋，配合PYCHARM或者VS CODE使用应该都会的 注释‘’’    ‘’’ “””  “”” 运算符算术运算符+加 -减 *乘 &#x2F;除 %求余 **乘方 &#x2F;&#x2F;取整数(向下取整，9&#x2F;&#x2F;2&#x3D;4) 位运算符&amp;按位与 |按位或  ^按位异或 ~按位取反 &lt;&lt;左移  &amp;g">
<meta property="og:type" content="article">
<meta property="og:title" content="python基本语法">
<meta property="og:url" content="https://heit0ng.gitee.io/2022/02/19/python/index.html">
<meta property="og:site_name" content="heit0ng">
<meta property="og:description" content="看了下课表这学期要学python了，我就把之前学的时候做的笔记传上来吧 比C语言之类确实简单不少，所以笔记做得很简陋，配合PYCHARM或者VS CODE使用应该都会的 注释‘’’    ‘’’ “””  “”” 运算符算术运算符+加 -减 *乘 &#x2F;除 %求余 **乘方 &#x2F;&#x2F;取整数(向下取整，9&#x2F;&#x2F;2&#x3D;4) 位运算符&amp;按位与 |按位或  ^按位异或 ~按位取反 &lt;&lt;左移  &amp;g">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2022/02/19/Q9FRIvSuonhgz4E.jpg">
<meta property="article:published_time" content="2022-02-19T08:46:20.050Z">
<meta property="article:modified_time" content="2022-02-19T08:56:52.128Z">
<meta property="article:author" content="heit0ng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/02/19/Q9FRIvSuonhgz4E.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://heit0ng.gitee.io/2022/02/19/python/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'python基本语法',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-02-19 16:56:52'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2022/02/19/Q9FRIvSuonhgz4E.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">heit0ng</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">python基本语法</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-19T08:46:20.050Z" title="发表于 2022-02-19 16:46:20">2022-02-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-19T08:56:52.128Z" title="更新于 2022-02-19 16:56:52">2022-02-19</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="python基本语法"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>看了下课表这学期要学python了，我就把之前学的时候做的笔记传上来吧</p>
<p>比C语言之类确实简单不少，所以笔记做得很简陋，配合PYCHARM或者VS CODE使用应该都会的</p>
<h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><h1 id=""><a href="#" class="headerlink" title=""></a></h1><p>‘’’    ‘’’</p>
<p>“””  “””</p>
<h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3><h4 id="算术运算符"><a href="#算术运算符" class="headerlink" title="算术运算符"></a>算术运算符</h4><p>+加 -减 *乘 /除 %求余 **乘方 //取整数(向下取整，9//2=4)</p>
<h4 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h4><p>&amp;按位与 |按位或  ^按位异或 ~按位取反 &lt;&lt;左移  &gt;&gt;右移</p>
<h4 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h4><p>and or not</p>
<h4 id="python成员运算符"><a href="#python成员运算符" class="headerlink" title="python成员运算符"></a>python成员运算符</h4><p>in和not in</p>
<h4 id="python身份运算符"><a href="#python身份运算符" class="headerlink" title="python身份运算符"></a>python身份运算符</h4><p>is和is not</p>
<h4 id="Python运算符优先级"><a href="#Python运算符优先级" class="headerlink" title="Python运算符优先级"></a>Python运算符优先级</h4><p>以下表格列出了从最高到最低优先级的所有运算符：</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>**</td>
<td>指数 (最高优先级)</td>
</tr>
<tr>
<td>~ + -</td>
<td>按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)</td>
</tr>
<tr>
<td>* / % //</td>
<td>乘，除，求余数和取整除</td>
</tr>
<tr>
<td>+ -</td>
<td>加法减法</td>
</tr>
<tr>
<td>&gt;&gt; &lt;&lt;</td>
<td>右移，左移运算符</td>
</tr>
<tr>
<td>&amp;</td>
<td>位 ‘AND’</td>
</tr>
<tr>
<td>^ |</td>
<td>位运算符</td>
</tr>
<tr>
<td>&lt;= &lt; &gt; &gt;=</td>
<td>比较运算符</td>
</tr>
<tr>
<td>== !=</td>
<td>等于运算符</td>
</tr>
<tr>
<td>= %= /= //= -= += *= **=</td>
<td>赋值运算符</td>
</tr>
<tr>
<td>is is not</td>
<td>身份运算符</td>
</tr>
<tr>
<td>in not in</td>
<td>成员运算符</td>
</tr>
<tr>
<td>not and or</td>
<td>逻辑运算符</td>
</tr>
</tbody></table>
<p>not&gt;and&gt;or</p>
<h3 id="数字"><a href="#数字" class="headerlink" title="数字"></a>数字</h3><p>整型(int) 浮点型(float) 复数(complex)或者a+bj，a实部，b虚部</p>
<h4 id="数字类型转换"><a href="#数字类型转换" class="headerlink" title="数字类型转换"></a>数字类型转换</h4><p>与c差不多，complex(x,y)x实部，y虚部可无</p>
<h4 id="数学函数"><a href="#数学函数" class="headerlink" title="数学函数"></a>数学函数</h4><table>
<thead>
<tr>
<th>函数</th>
<th>返回值 ( 描述 )</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-abs.html">abs(x)</a></td>
<td>返回数字的绝对值，如abs(-10) 返回 10</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-ceil.html">ceil(x) </a></td>
<td>返回数字的上入整数，如math.ceil(4.1) 返回 5</td>
</tr>
<tr>
<td>cmp(x, y)</td>
<td>如果 x &lt; y 返回 -1, 如果 x == y 返回 0, 如果 x &gt; y 返回 1。 <strong>Python 3 已废弃，使用 (x&gt;y)-(x&lt;y) 替换</strong>。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-exp.html">exp(x) </a></td>
<td>返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-fabs.html">fabs(x)</a></td>
<td>返回数字的绝对值，如math.fabs(-10) 返回10.0</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-floor.html">floor(x) </a></td>
<td>返回数字的下舍整数，如math.floor(4.9)返回 4</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-log.html">log(x) </a></td>
<td>如math.log(math.e)返回1.0,math.log(100,10)返回2.0</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-log10.html">log10(x) </a></td>
<td>返回以10为基数的x的对数，如math.log10(100)返回 2.0</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-max.html">max(x1, x2,…) </a></td>
<td>返回给定参数的最大值，参数可以为序列。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-min.html">min(x1, x2,…) </a></td>
<td>返回给定参数的最小值，参数可以为序列。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-modf.html">modf(x) </a></td>
<td>返回x的整数部分与小数部分，两部分的数值符号与x相同，整数部分以浮点型表示。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-pow.html">pow(x, y)</a></td>
<td>x**y 运算后的值。</td>
</tr>
<tr>
<td>[round(x <a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-round.html">,n])</a></td>
<td>返回浮点数 x 的四舍五入值，如给出 n 值，则代表舍入到小数点后的位数。 <strong>其实准确的说是保留值将保留到离上一位更近的一端。</strong></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-sqrt.html">sqrt(x) </a></td>
<td>返回数字x的平方根。</td>
</tr>
</tbody></table>
<h4 id="随机数函数"><a href="#随机数函数" class="headerlink" title="随机数函数"></a>随机数函数</h4><table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-choice.html">choice(seq)</a></td>
<td>从序列的元素中随机挑选一个元素，比如random.choice(range(10))，从0到9中随机挑选一个整数。</td>
</tr>
<tr>
<td>[randrange (<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-randrange.html">start,] stop [,step]) </a></td>
<td>从指定范围内，按指定基数递增的集合中获取一个随机数，基数默认值为 1</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-random.html">random() </a></td>
<td>随机生成下一个实数，它在[0,1)范围内。</td>
</tr>
<tr>
<td>[seed(<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-seed.html">x]) </a></td>
<td>改变随机数生成器的种子seed。如果你不了解其原理，你不必特别去设定seed，Python会帮你选择seed。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-shuffle.html">shuffle(lst) </a></td>
<td>将序列的所有元素随机排序</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-func-number-uniform.html">uniform(x, y)</a></td>
<td>随机生成下一个实数，它在[x,y]范围内。</td>
</tr>
</tbody></table>
<h3 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h3><p>字符串可以用 + 运算符连接在一起，用 * 运算符重复</p>
<p>print(‘hello\nrunoob’)      # 使用反斜杠()+n转义特殊字符，会换行</p>
<p>print(r’hello\nrunoob’)     # 在字符串前面添加一个 r，表示原始字符串，不会发生转义，不会换行</p>
<h4 id="字符串的截取或访问的语法："><a href="#字符串的截取或访问的语法：" class="headerlink" title="字符串的截取或访问的语法："></a>字符串的截取或访问的语法：</h4><p>变量[头下标:尾下标:步长]约等于区间中左闭右开，与C语言的[]类似<img src="https://static.runoob.com/wp-content/uploads/123456-20200923-1.svg" alt="img"></p>
<h4 id="python转义字符"><a href="#python转义字符" class="headerlink" title="python转义字符"></a>python转义字符</h4><table>
<thead>
<tr>
<th>转义字符</th>
<th>描述</th>
<th>实例</th>
</tr>
</thead>
<tbody><tr>
<td>\(在行尾时)</td>
<td>续行符</td>
<td><code>&gt;&gt;&gt; print(&quot;line1 \ ... line2 \ ... line3&quot;) line1 line2 line3 &gt;&gt;&gt; </code></td>
</tr>
<tr>
<td>\</td>
<td>反斜杠符号</td>
<td><code>&gt;&gt;&gt; print(&quot;\\&quot;) \</code></td>
</tr>
<tr>
<td>&#39;</td>
<td>单引号</td>
<td><code>&gt;&gt;&gt; print(&#39;\&#39;&#39;) &#39;</code></td>
</tr>
<tr>
<td>&quot;</td>
<td>双引号</td>
<td><code>&gt;&gt;&gt; print(&quot;\&quot;&quot;) &quot;</code></td>
</tr>
<tr>
<td>\a</td>
<td>响铃</td>
<td><code>&gt;&gt;&gt; print(&quot;\a&quot;)</code>执行后电脑有响声。</td>
</tr>
<tr>
<td>\b</td>
<td>退格(Backspace)</td>
<td><code>&gt;&gt;&gt; print(&quot;Hello \b World!&quot;) Hello World!</code></td>
</tr>
<tr>
<td>\000</td>
<td>空</td>
<td><code>&gt;&gt;&gt; print(&quot;\000&quot;) &gt;&gt;&gt; </code></td>
</tr>
<tr>
<td>\n</td>
<td>换行</td>
<td><code>&gt;&gt;&gt; print(&quot;\n&quot;)  &gt;&gt;&gt;</code></td>
</tr>
<tr>
<td>\v</td>
<td>纵向制表符</td>
<td><code>&gt;&gt;&gt; print(&quot;Hello \v World!&quot;) Hello        World! &gt;&gt;&gt;</code></td>
</tr>
<tr>
<td>\t</td>
<td>横向制表符</td>
<td><code>&gt;&gt;&gt; print(&quot;Hello \t World!&quot;) Hello    World! &gt;&gt;&gt;</code></td>
</tr>
<tr>
<td>\r</td>
<td>回车，将 \r 后面的内容移到字符串开头，并逐一替换开头部分的字符，直至将 \r 后面的内容完全替换完成。</td>
<td><code>&gt;&gt;&gt; print(&quot;Hello\rWorld!&quot;) World! &gt;&gt;&gt; print(&#39;google runoob taobao\r123456&#39;) 123456 runoob taobao</code></td>
</tr>
</tbody></table>
<h4 id="Python-字符串格式化"><a href="#Python-字符串格式化" class="headerlink" title="Python 字符串格式化"></a>Python 字符串格式化</h4><p>print (“我叫 %s 今年 %d 岁!” % (‘小明’, 10))可以先将括号内的内容赋值给变量(与C语言差不了太多)</p>
<h4 id="f-string-上面的新表示方法py3-6之后的-例如"><a href="#f-string-上面的新表示方法py3-6之后的-例如" class="headerlink" title="f-string(上面的新表示方法py3.6之后的)例如:"></a>f-string(上面的新表示方法py3.6之后的)例如:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a= &#x27;klee&#x27;</span><br><span class="line">b= 10</span><br><span class="line">print(f&quot;my name is &#123;a&#125; &#123;b&#125; years old&quot;)</span><br></pre></td></tr></table></figure>

<h4 id="Python-的字符串内建函数"><a href="#Python-的字符串内建函数" class="headerlink" title="Python 的字符串内建函数"></a>Python 的字符串内建函数</h4><table>
<thead>
<tr>
<th>序号</th>
<th>方法及描述</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-capitalize.html">capitalize()</a> 将字符串的第一个字符转换为大写</td>
</tr>
<tr>
<td>2</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-center.html">center(width, fillchar)</a> 返回一个指定的宽度 width 居中的字符串，fillchar 为填充的字符，默认为空格。</td>
</tr>
<tr>
<td>3</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-count.html">count(str, beg= 0,end=len(string))</a> 返回 str 在 string 里面出现的次数，如果 beg 或者 end 指定则返回指定范围内 str 出现的次数</td>
</tr>
<tr>
<td>4</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-decode.html">bytes.decode(encoding=”utf-8”, errors=”strict”)</a> Python3 中没有 decode 方法，但我们可以使用 bytes 对象的 decode() 方法来解码给定的 bytes 对象，这个 bytes 对象可以由 str.encode() 来编码返回。</td>
</tr>
<tr>
<td>5</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-encode.html">encode(encoding=’UTF-8’,errors=’strict’)</a> 以 encoding 指定的编码格式编码字符串，如果出错默认报一个ValueError 的异常，除非 errors 指定的是’ignore’或者’replace’</td>
</tr>
<tr>
<td>6</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-endswith.html">endswith(suffix, beg=0, end=len(string))</a> 检查字符串是否以 obj 结束，如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束，如果是，返回 True,否则返回 False.</td>
</tr>
<tr>
<td>7</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-expandtabs.html">expandtabs(tabsize=8)</a> 把字符串 string 中的 tab 符号转为空格，tab 符号默认的空格数是 8 。</td>
</tr>
<tr>
<td>8</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-find.html">find(str, beg=0, end=len(string))</a> 检测 str 是否包含在字符串中，如果指定范围 beg 和 end ，则检查是否包含在指定范围内，如果包含返回开始的索引值，否则返回-1</td>
</tr>
<tr>
<td>9</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-index.html">index(str, beg=0, end=len(string))</a> 跟find()方法一样，只不过如果str不在字符串中会报一个异常。</td>
</tr>
<tr>
<td>10</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-isalnum.html">isalnum()</a> 如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True，否则返回 False</td>
</tr>
<tr>
<td>11</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-isalpha.html">isalpha()</a> 如果字符串至少有一个字符并且所有字符都是字母或中文字则返回 True, 否则返回 False</td>
</tr>
<tr>
<td>12</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-isdigit.html">isdigit()</a> 如果字符串只包含数字则返回 True 否则返回 False..</td>
</tr>
<tr>
<td>13</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-islower.html">islower()</a> 如果字符串中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是小写，则返回 True，否则返回 False</td>
</tr>
<tr>
<td>14</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-isnumeric.html">isnumeric()</a> 如果字符串中只包含数字字符，则返回 True，否则返回 False</td>
</tr>
<tr>
<td>15</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-isspace.html">isspace()</a> 如果字符串中只包含空白，则返回 True，否则返回 False.</td>
</tr>
<tr>
<td>16</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-istitle.html">istitle()</a>      如果字符串是标题化的(见 title())则返回 True，否则返回 False</td>
</tr>
<tr>
<td>17</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-isupper.html">isupper()</a> 如果字符串中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是大写，则返回 True，否则返回 False</td>
</tr>
<tr>
<td>18</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-join.html">join(seq)</a> 以指定字符串作为分隔符，将 seq 中所有的元素(的字符串表示)合并为一个新的字符串</td>
</tr>
<tr>
<td>19</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-len.html">len(string)</a> 返回字符串长度</td>
</tr>
<tr>
<td>20</td>
<td>[ljust(width<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-ljust.html">, fillchar])</a> 返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串，fillchar 默认为空格。</td>
</tr>
<tr>
<td>21</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-lower.html">lower()</a> 转换字符串中所有大写字符为小写.</td>
</tr>
<tr>
<td>22</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-lstrip.html">lstrip()</a> 截掉字符串左边的空格或指定字符。</td>
</tr>
<tr>
<td>23</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-maketrans.html">maketrans()</a> 创建字符映射的转换表，对于接受两个参数的最简单的调用方式，第一个参数是字符串，表示需要转换的字符，第二个参数也是字符串表示转换的目标。</td>
</tr>
<tr>
<td>24</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-max.html">max(str)</a> 返回字符串 str 中最大的字母。</td>
</tr>
<tr>
<td>25</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-min.html">min(str)</a> 返回字符串 str 中最小的字母。</td>
</tr>
<tr>
<td>26</td>
<td>[replace(old, new <a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-replace.html">, max])</a> 把 将字符串中的 old 替换成 new,如果 max 指定，则替换不超过 max 次。</td>
</tr>
<tr>
<td>27</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-rfind.html">rfind(str, beg=0,end=len(string))</a> 类似于 find()函数，不过是从右边开始查找.</td>
</tr>
<tr>
<td>28</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-rindex.html">rindex( str, beg=0, end=len(string))</a> 类似于 index()，不过是从右边开始.</td>
</tr>
<tr>
<td>29</td>
<td>[rjust(width,<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-rjust.html">, fillchar])</a> 返回一个原字符串右对齐,并使用fillchar(默认空格）填充至长度 width 的新字符串</td>
</tr>
<tr>
<td>30</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-rstrip.html">rstrip()</a> 删除字符串末尾的空格或指定字符。</td>
</tr>
<tr>
<td>31</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-split.html">split(str=””, num=string.count(str))</a>  以 str 为分隔符截取字符串，如果 num 有指定值，则仅截取 num+1 个子字符串</td>
</tr>
<tr>
<td>32</td>
<td>[splitlines(<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-splitlines.html">keepends])</a> 按照行(‘\r’, ‘\r\n’, \n’)分隔，返回一个包含各行作为元素的列表，如果参数 keepends 为 False，不包含换行符，如果为 True，则保留换行符。</td>
</tr>
<tr>
<td>33</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-startswith.html">startswith(substr, beg=0,end=len(string))</a> 检查字符串是否是以指定子字符串 substr 开头，是则返回 True，否则返回 False。如果beg 和 end 指定值，则在指定范围内检查。</td>
</tr>
<tr>
<td>34</td>
<td>[strip(<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-strip.html">chars])</a> 在字符串上执行 lstrip()和 rstrip()</td>
</tr>
<tr>
<td>35</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-swapcase.html">swapcase()</a> 将字符串中大写转换为小写，小写转换为大写</td>
</tr>
<tr>
<td>36</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-title.html">title()</a> 返回”标题化”的字符串,就是说所有单词都是以大写开始，其余字母均为小写(见 istitle())</td>
</tr>
<tr>
<td>37</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-translate.html">translate(table, deletechars=””)</a> 根据 str 给出的表(包含 256 个字符)转换 string 的字符, 要过滤掉的字符放到 deletechars 参数中</td>
</tr>
<tr>
<td>38</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-upper.html">upper()</a>      转换字符串中的小写字母为大写</td>
</tr>
<tr>
<td>39</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-zfill.html">zfill (width)</a> 返回长度为 width 的字符串，原字符串右对齐，前面填充0</td>
</tr>
<tr>
<td>40</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-string-isdecimal.html">isdecimal()</a>  检查字符串是否只包含十进制字符，如果是返回 true，否则返回 false。</td>
</tr>
</tbody></table>
<h3 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h3><p>例如:list = [1,2,3,4,5,’’google’’,’green’]</p>
<p>同样使用[ ]方括号进行节选,和C语言的数组使用方法大致一样</p>
<h4 id="删除列表元素"><a href="#删除列表元素" class="headerlink" title="删除列表元素"></a>删除列表元素</h4><p>如del list[2]</p>
<h4 id="Python列表脚本操作符"><a href="#Python列表脚本操作符" class="headerlink" title="Python列表脚本操作符"></a>Python列表脚本操作符</h4><p>列表对 + 和  * 的操作符与字符串相似。+ 号用于组合列表，* 号用于重复列表。</p>
<p>如下所示：</p>
<table>
<thead>
<tr>
<th>Python 表达式</th>
<th>结果</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>len([1, 2, 3])</td>
<td>3</td>
<td>长度</td>
</tr>
<tr>
<td>[1, 2, 3] + [4, 5, 6]</td>
<td>[1, 2, 3, 4, 5, 6]</td>
<td>组合</td>
</tr>
<tr>
<td>[‘Hi!’] * 4</td>
<td>[‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’]</td>
<td>重复</td>
</tr>
<tr>
<td>3 in [1, 2, 3]</td>
<td>True</td>
<td>元素是否存在于列表中</td>
</tr>
<tr>
<td>for x in [1, 2, 3]: print(x, end=” “)</td>
<td>1 2 3</td>
<td>迭代</td>
</tr>
</tbody></table>
<h4 id="列表可以嵌套"><a href="#列表可以嵌套" class="headerlink" title="列表可以嵌套"></a>列表可以嵌套</h4><p>a = [‘a’, ‘b’, ‘c’], n = [1, 2, 3],x = [a, n]得到&gt;&gt;&gt; x[[‘a’, ‘b’, ‘c’], [1, 2, 3]]</p>
<p>x[0] [‘a’, ‘b’, ‘c’]   x[ 0 ][1 】’b’</p>
<h4 id="Python列表函数-amp-方法"><a href="#Python列表函数-amp-方法" class="headerlink" title="Python列表函数&amp;方法"></a>Python列表函数&amp;方法</h4><p>Python包含以下函数:</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-len.html">len(list)</a> 列表元素个数</td>
</tr>
<tr>
<td>2</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-max.html">max(list)</a> 返回列表元素最大值</td>
</tr>
<tr>
<td>3</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-min.html">min(list)</a> 返回列表元素最小值</td>
</tr>
<tr>
<td>4</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-list.html">list(seq)</a> 将元组转换为列表</td>
</tr>
</tbody></table>
<p>Python包含以下方法:</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>方法</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-append.html">list.append(obj)</a> 在列表末尾添加新的对象</td>
</tr>
<tr>
<td>2</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-count.html">list.count(obj)</a> 统计某个元素在列表中出现的次数</td>
</tr>
<tr>
<td>3</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-extend.html">list.extend(seq)</a> 在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）</td>
</tr>
<tr>
<td>4</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-index.html">list.index(obj)</a> 从列表中找出某个值第一个匹配项的索引位置</td>
</tr>
<tr>
<td>5</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-insert.html">list.insert(index, obj)</a> 将对象插入列表</td>
</tr>
<tr>
<td>6</td>
<td>[list.pop(<a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-pop.html">index=-1])</a> 移除列表中的一个元素（默认最后一个元素），并且返回该元素的值</td>
</tr>
<tr>
<td>7</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-remove.html">list.remove(obj)</a> 移除列表中某个值的第一个匹配项</td>
</tr>
<tr>
<td>8</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-reverse.html">list.reverse()</a> 反向列表中元素</td>
</tr>
<tr>
<td>9</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-sort.html">    list.sort( key=None, reverse=False)</a> 对原列表进行排序</td>
</tr>
<tr>
<td>10</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-clear.html">list.clear()</a> 清空列表</td>
</tr>
<tr>
<td>11</td>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-att-list-copy.html">list.copy()</a> 复制列表</td>
</tr>
</tbody></table>
<h3 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h3><p>与列表类似，但是元素不能修改(可以拼接)，用小括号(  )表示</p>
<p>其他参考列表</p>
<h3 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h3><p>用花括号表示{ }存在键值对 key:value key是唯一，value不是唯一</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2016/04/py-dict-3.png" alt="img"></p>
<h4 id="访问字典的值"><a href="#访问字典的值" class="headerlink" title="访问字典的值"></a>访问字典的值</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = &#123;&#x27;abc&#x27;:321,&#x27;cba&#x27;:123&#125;</span><br><span class="line">print(a[&#x27;abc&#x27;])</span><br></pre></td></tr></table></figure>

<h4 id="删除字典元素"><a href="#删除字典元素" class="headerlink" title="删除字典元素"></a>删除字典元素</h4><p>del tinydict[‘Name’] # 删除键 </p>
<p>‘Name’ tinydict.clear()     # 清空字典 </p>
<p>del tinydict         # 删除字典</p>
<p>其他参考C语言数组</p>
<h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><p>可以用大括号{ }，但最好使用set()，set(value1,value2…..),同样参考下数组用法</p>
<h4 id="集合的运算"><a href="#集合的运算" class="headerlink" title="集合的运算"></a>集合的运算</h4><p>参考数学的集合运算</p>
<p>a-b a中包含但b不包含</p>
<p>a|b a与b的并集</p>
<p>a&amp;b a与b的交集</p>
<p>a^b 不同时存在于a,b</p>
<h4 id="基本操作"><a href="#基本操作" class="headerlink" title="基本操作"></a>基本操作</h4><h5 id="1-添加元素"><a href="#1-添加元素" class="headerlink" title="1.添加元素"></a>1.添加元素</h5><p>s.add(x)将x添加到集合s中，如果元素为列表，元组等使用s.update(x)参数可多个，加在开头</p>
<h5 id="2-移除元素"><a href="#2-移除元素" class="headerlink" title="2.移除元素"></a>2.移除元素</h5><p>s.remove(x)如果x不存在会发生错误</p>
<p>s.discard(x)x不存在也不会发生错误</p>
<p>s.pop()随机删除一个元素</p>
<h5 id="3-统计元素个数"><a href="#3-统计元素个数" class="headerlink" title="3.统计元素个数"></a>3.统计元素个数</h5><p>len(s)</p>
<h5 id="4-清空集合"><a href="#4-清空集合" class="headerlink" title="4.清空集合"></a>4.清空集合</h5><p>s.clear()</p>
<h5 id="集合内置方法完整列表-其他操作"><a href="#集合内置方法完整列表-其他操作" class="headerlink" title="集合内置方法完整列表(其他操作)"></a>集合内置方法完整列表(其他操作)</h5><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-add.html">add()</a></td>
<td>为集合添加元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-clear.html">clear()</a></td>
<td>移除集合中的所有元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-copy.html">copy()</a></td>
<td>拷贝一个集合</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-difference.html">difference()</a></td>
<td>返回多个集合的差集</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-difference_update.html">difference_update()</a></td>
<td>移除集合中的元素，该元素在指定的集合也存在。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-discard.html">discard()</a></td>
<td>删除集合中指定的元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-intersection.html">intersection()</a></td>
<td>返回集合的交集</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-intersection_update.html">intersection_update()</a></td>
<td>返回集合的交集。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-isdisjoint.html">isdisjoint()</a></td>
<td>判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-issubset.html">issubset()</a></td>
<td>判断指定集合是否为该方法参数集合的子集。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-issuperset.html">issuperset()</a></td>
<td>判断该方法的参数集合是否为指定集合的子集</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-pop.html">pop()</a></td>
<td>随机移除元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-remove.html">remove()</a></td>
<td>移除指定元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-symmetric_difference.html">symmetric_difference()</a></td>
<td>返回两个集合中不重复的元素集合。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-symmetric_difference_update.html">symmetric_difference_update()</a></td>
<td>移除当前集合中在另外一个指定集合相同的元素，并将另外一个指定集合中不同的元素插入到当前集合中。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-union.html">union()</a></td>
<td>返回两个集合的并集</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/ref-set-update.html">update()</a></td>
<td>给集合添加元素</td>
</tr>
</tbody></table>
<h3 id="特殊代码¿"><a href="#特殊代码¿" class="headerlink" title="特殊代码¿"></a>特殊代码¿</h3><h4 id="end关键字"><a href="#end关键字" class="headerlink" title="end关键字"></a>end关键字</h4><p>例如</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">for i in range(8):</span><br><span class="line">    print(i,end=&#x27;&#x27;)</span><br></pre></td></tr></table></figure>

<p>就不会自动换行也可以end=’,’等等</p>
<h3 id="条件语句"><a href="#条件语句" class="headerlink" title="条件语句"></a>条件语句</h3><p>if elif else</p>
<p>if a==100:</p>
<p>​    语句1</p>
<p>其余参考C语言</p>
<h3 id="循环语句"><a href="#循环语句" class="headerlink" title="循环语句"></a>循环语句</h3><h4 id="while-参考-没有do-while"><a href="#while-参考-没有do-while" class="headerlink" title="while,参考(没有do..while)"></a>while,参考(没有do..while)</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a = 1</span><br><span class="line">while a&lt;10:</span><br><span class="line">    print(a,end=&#x27;,&#x27;)</span><br><span class="line">    a+=2</span><br></pre></td></tr></table></figure>

<h5 id="while中使用else"><a href="#while中使用else" class="headerlink" title="while中使用else"></a>while中使用else</h5><p>当while条件不成立时，执行else语句，如</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">a = 1</span><br><span class="line">sum = 0</span><br><span class="line">while a&lt;10:</span><br><span class="line">    print(a,end=&#x27;,&#x27;)</span><br><span class="line">    sum+=a</span><br><span class="line">    a+=2</span><br><span class="line">else:</span><br><span class="line">    print(sum)</span><br></pre></td></tr></table></figure>

<h4 id="for语句"><a href="#for语句" class="headerlink" title="for语句"></a>for语句</h4><p>for <variable> in <sequence>:  </p>
  <statements> 

<p>else:</p>
<p>​    <statements></p>
<p>例如:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = [123,&#x27;google&#x27;,&#x27;bilibili&#x27;,&#x27;baidu&#x27;]</span><br><span class="line">for i in a:</span><br><span class="line">    print(i)</span><br><span class="line">else:</span><br><span class="line">    print(a[3])</span><br></pre></td></tr></table></figure>

<h4 id="break和continue"><a href="#break和continue" class="headerlink" title="break和continue"></a>break和continue</h4><p>参考C语言</p>
<h4 id="range-函数"><a href="#range-函数" class="headerlink" title="range()函数"></a>range()函数</h4><p>range(起始，终止，步长)左闭右开</p>
<h4 id="pass"><a href="#pass" class="headerlink" title="pass"></a>pass</h4><p>空语句</p>
<h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><p><img src="https://s2.loli.net/2022/02/19/OLA2KmFP3tRk6BM.png" alt="python.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">def 函数名（参数列表）:</span><br><span class="line">    函数体</span><br></pre></td></tr></table></figure>

<p>例如:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def hello():</span><br><span class="line">    print(&#x27;hello world!&#x27;)</span><br><span class="line"></span><br><span class="line">hello()</span><br></pre></td></tr></table></figure>

<p>传参有些许不同</p>
<p>def aaaa(name,age):</p>
<p>​        xxxxxx</p>
<p>aaaa(name=’klee’,age=18)</p>
<h4 id="可更改-mutable-与不可更改-immutable-对象"><a href="#可更改-mutable-与不可更改-immutable-对象" class="headerlink" title="可更改(mutable)与不可更改(immutable)对象"></a>可更改(mutable)与不可更改(immutable)对象</h4><p>在 python 中，strings, tuples, 和 numbers 是不可更改的对象，而 list,dict 等则是可以修改的对象。</p>
<ul>
<li><strong>不可变类型：</strong>变量赋值 <strong>a=5</strong> 后再赋值 <strong>a=10</strong>，这里实际是新生成一个 int 值对象 10，再让 a 指向它，而 5 被丢弃，不是改变 a 的值，相当于新生成了 a。</li>
<li><strong>可变类型：</strong>变量赋值 <strong>la=[1,2,3,4]</strong> 后再赋值 <strong>la[2]=5</strong> 则是将 list la 的第三个元素值更改，本身la没有动，只是其内部的一部分值被修改了。</li>
</ul>
<p>python 函数的参数传递：</p>
<ul>
<li><strong>不可变类型：</strong>类似 C++ 的值传递，如整数、字符串、元组。如 fun(a)，传递的只是 a 的值，没有影响 a 对象本身。如果在 fun(a) 内部修改 a 的值，则是新生成一个 a 的对象。</li>
<li><strong>可变类型：</strong>类似 C++ 的引用传递，如 列表，字典。如 fun(la)，则是将 la 真正的传过去，修改后 fun 外部的 la 也会受影响</li>
<li>(整体参考C语言函数传参,C语言传参理解就可，可变类型传的是数组)</li>
</ul>
<p>其余大致参考C语言</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">heit0ng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://heit0ng.gitee.io/2022/02/19/python/">https://heit0ng.gitee.io/2022/02/19/python/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://heit0ng.gitee.io" target="_blank">heit0ng</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2022/02/19/Q9FRIvSuonhgz4E.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/22/%E6%94%BB%E9%98%B2%E4%B8%96%E7%95%8C%E7%9A%84wp/"><img class="prev-cover" src="https://s2.loli.net/2022/02/22/Wy6vfC23Rpqx8L4.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">攻防世界的wp(一)</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/11/WIN32/"><img class="next-cover" src="https://s2.loli.net/2022/02/11/zaOwVjM52ZRoAh3.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">win32基础</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">heit0ng</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A"><span class="toc-number">1.</span> <span class="toc-text">注释</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number"></span> <span class="toc-text"></span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.</span> <span class="toc-text">运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.1.</span> <span class="toc-text">算术运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%8D%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.2.</span> <span class="toc-text">位运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.3.</span> <span class="toc-text">逻辑运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#python%E6%88%90%E5%91%98%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.4.</span> <span class="toc-text">python成员运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#python%E8%BA%AB%E4%BB%BD%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.5.</span> <span class="toc-text">python身份运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Python%E8%BF%90%E7%AE%97%E7%AC%A6%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-number">1.6.</span> <span class="toc-text">Python运算符优先级</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%AD%97"><span class="toc-number">2.</span> <span class="toc-text">数字</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E5%AD%97%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.1.</span> <span class="toc-text">数字类型转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0"><span class="toc-number">2.2.</span> <span class="toc-text">数学函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0"><span class="toc-number">2.3.</span> <span class="toc-text">随机数函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.</span> <span class="toc-text">字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%88%AA%E5%8F%96%E6%88%96%E8%AE%BF%E9%97%AE%E7%9A%84%E8%AF%AD%E6%B3%95%EF%BC%9A"><span class="toc-number">3.1.</span> <span class="toc-text">字符串的截取或访问的语法：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#python%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="toc-number">3.2.</span> <span class="toc-text">python转义字符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Python-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">3.3.</span> <span class="toc-text">Python 字符串格式化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#f-string-%E4%B8%8A%E9%9D%A2%E7%9A%84%E6%96%B0%E8%A1%A8%E7%A4%BA%E6%96%B9%E6%B3%95py3-6%E4%B9%8B%E5%90%8E%E7%9A%84-%E4%BE%8B%E5%A6%82"><span class="toc-number">3.4.</span> <span class="toc-text">f-string(上面的新表示方法py3.6之后的)例如:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Python-%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%86%85%E5%BB%BA%E5%87%BD%E6%95%B0"><span class="toc-number">3.5.</span> <span class="toc-text">Python 的字符串内建函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E8%A1%A8"><span class="toc-number">4.</span> <span class="toc-text">列表</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0"><span class="toc-number">4.1.</span> <span class="toc-text">删除列表元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Python%E5%88%97%E8%A1%A8%E8%84%9A%E6%9C%AC%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="toc-number">4.2.</span> <span class="toc-text">Python列表脚本操作符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E5%8F%AF%E4%BB%A5%E5%B5%8C%E5%A5%97"><span class="toc-number">4.3.</span> <span class="toc-text">列表可以嵌套</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0-amp-%E6%96%B9%E6%B3%95"><span class="toc-number">4.4.</span> <span class="toc-text">Python列表函数&amp;方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%83%E7%BB%84"><span class="toc-number">5.</span> <span class="toc-text">元组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E5%85%B8"><span class="toc-number">6.</span> <span class="toc-text">字典</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E5%AD%97%E5%85%B8%E7%9A%84%E5%80%BC"><span class="toc-number">6.1.</span> <span class="toc-text">访问字典的值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%AD%97%E5%85%B8%E5%85%83%E7%B4%A0"><span class="toc-number">6.2.</span> <span class="toc-text">删除字典元素</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E5%90%88"><span class="toc-number">7.</span> <span class="toc-text">集合</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E7%9A%84%E8%BF%90%E7%AE%97"><span class="toc-number">7.1.</span> <span class="toc-text">集合的运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="toc-number">7.2.</span> <span class="toc-text">基本操作</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0"><span class="toc-number">7.2.1.</span> <span class="toc-text">1.添加元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0"><span class="toc-number">7.2.2.</span> <span class="toc-text">2.移除元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E7%BB%9F%E8%AE%A1%E5%85%83%E7%B4%A0%E4%B8%AA%E6%95%B0"><span class="toc-number">7.2.3.</span> <span class="toc-text">3.统计元素个数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-%E6%B8%85%E7%A9%BA%E9%9B%86%E5%90%88"><span class="toc-number">7.2.4.</span> <span class="toc-text">4.清空集合</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95%E5%AE%8C%E6%95%B4%E5%88%97%E8%A1%A8-%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C"><span class="toc-number">7.2.5.</span> <span class="toc-text">集合内置方法完整列表(其他操作)</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%89%B9%E6%AE%8A%E4%BB%A3%E7%A0%81%C2%BF"><span class="toc-number">8.</span> <span class="toc-text">特殊代码¿</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#end%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">8.1.</span> <span class="toc-text">end关键字</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5"><span class="toc-number">9.</span> <span class="toc-text">条件语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="toc-number">10.</span> <span class="toc-text">循环语句</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#while-%E5%8F%82%E8%80%83-%E6%B2%A1%E6%9C%89do-while"><span class="toc-number">10.1.</span> <span class="toc-text">while,参考(没有do..while)</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#while%E4%B8%AD%E4%BD%BF%E7%94%A8else"><span class="toc-number">10.1.1.</span> <span class="toc-text">while中使用else</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#for%E8%AF%AD%E5%8F%A5"><span class="toc-number">10.2.</span> <span class="toc-text">for语句</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#break%E5%92%8Ccontinue"><span class="toc-number">10.3.</span> <span class="toc-text">break和continue</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#range-%E5%87%BD%E6%95%B0"><span class="toc-number">10.4.</span> <span class="toc-text">range()函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#pass"><span class="toc-number">10.5.</span> <span class="toc-text">pass</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-number">11.</span> <span class="toc-text">函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%AF%E6%9B%B4%E6%94%B9-mutable-%E4%B8%8E%E4%B8%8D%E5%8F%AF%E6%9B%B4%E6%94%B9-immutable-%E5%AF%B9%E8%B1%A1"><span class="toc-number">11.1.</span> <span class="toc-text">可更改(mutable)与不可更改(immutable)对象</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(2)/" title="j2ee(2)"><img src="https://s2.loli.net/2023/03/23/mTDuOeL67MFtgZs.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(2)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(2)/" title="j2ee(2)">j2ee(2)</a><time datetime="2023-03-22T15:50:01.860Z" title="发表于 2023-03-22 23:50:01">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(1)/" title="j2ee(1)"><img src="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(1)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(1)/" title="j2ee(1)">j2ee(1)</a><time datetime="2023-03-22T12:20:17.683Z" title="发表于 2023-03-22 20:20:17">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统"><img src="https://s2.loli.net/2022/05/16/ae4E7n2D3I5mqkh.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C语言图书管理系统"/></a><div class="content"><a class="title" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统">C语言图书管理系统</a><time datetime="2022-05-16T14:28:31.730Z" title="发表于 2022-05-16 22:28:31">2022-05-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/07/C++qt/" title="C++Qt"><img src="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C++Qt"/></a><div class="content"><a class="title" href="/2022/04/07/C++qt/" title="C++Qt">C++Qt</a><time datetime="2022-04-07T12:41:19.618Z" title="发表于 2022-04-07 20:41:19">2022-04-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)"><img src="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="c++的知识(1)"/></a><div class="content"><a class="title" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)">c++的知识(1)</a><time datetime="2022-03-12T13:48:26.310Z" title="发表于 2022-03-12 21:48:26">2022-03-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By heit0ng</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>